package com.dgl.sdk.util;

import android.content.Context;
import com.dgl.sdk.MultiDownloader.MultiFileDownloader;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import net.tsz.afinal.http.HttpHandler;

import java.io.File;

/**
 * Created by dgl on 2014/4/4.
 */
public class FileDownloader {

    //使用 afinal 来下载 ， 还是multi 来下载
    private boolean USE_AFINL_DOWNLOAD = false;  //目前用afinal下载 ， 还有问题， 不能断点。
    private  boolean USE_MULTI_DOWNLOAD = true;

    private Context mContext;
    private MultiFileDownloader fd;

    public interface DownloadCallback{
        public void onStart();
        public void onProgress(int progress);
        public void onComplete(String filePath);
        public void onError(String error);
    }

    private String url;
    private String saveDir;
    private String fileName;
    private int threadNum;
    private DownloadCallback downloadCallback;

    //判断是否下载过了
    private boolean haveStartDownload = false;

    private FinalHttp fh;
    HttpHandler<File> handler;


    /**
     *
     * @param url
     * @param saveDir  保存地址
     * @param fileName  保存名称
     * @param threadNum  线程数  用不上  ！
     * @param downloadCallback  回调
     */
    public FileDownloader(Context context,String url, String saveDir, String fileName, int threadNum,
                          final DownloadCallback downloadCallback) {
        mContext = context;

        this.url = url;
        this.saveDir = saveDir;
        this.fileName = fileName;
        this.threadNum = threadNum;
        this.downloadCallback = downloadCallback;
    }


    public void download(){



        haveStartDownload = true;

        if(USE_AFINL_DOWNLOAD){
            fh = new FinalHttp();
            finalDownload();
        }else if(USE_MULTI_DOWNLOAD){
            fd = new MultiFileDownloader(mContext, url,saveDir,fileName,threadNum,new MultiFileDownloader.DownloadCallback() {
                @Override
                public void onStart() {
                    downloadCallback.onStart();
                }

                @Override
                public void onProgress(int progress) {
                    downloadCallback.onProgress(progress);
                }

                @Override
                public void onComplete(String filePath) {
                    downloadCallback.onComplete(filePath);
                }

                @Override
                public void onError(String error) {
                    downloadCallback.onError(error);
                }
            });
            multiDownload();
        }


    }

    private void multiDownload(){

        fd.download();
    }

    private void finalDownload(){

        handler = fh.download(url, new AjaxParams(), saveDir+fileName, true, new AjaxCallBack<File>() {


            @Override
            public void onStart() {
                super.onStart();
                downloadCallback.onStart();
            }

            @Override
            public void onLoading(long count, long current) {
                super.onLoading(count, current);

                downloadCallback.onProgress((int)(current*100 / count));
            }

            @Override
            public void onSuccess(File file) {
                super.onSuccess(file);
                downloadCallback.onComplete(file.getAbsolutePath());
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                downloadCallback.onError(strMsg);
            }
        }.progress(true, 500));  //500毫秒更新一次
    }


    public void setRun(boolean isRun){
        if(USE_AFINL_DOWNLOAD){
            if(isRun){
//            reDownLoad();  用不到，  用到也重复了。
            }else{
                handler.stop();
            }
        }else if(USE_MULTI_DOWNLOAD){
            fd.setRun(isRun);
        }

    }

    /**
     * 重新下载
     */
    public  void reDownLoad() {

        if(USE_AFINL_DOWNLOAD){
            if(haveStartDownload){

                finalDownload();
            }
        }else if(USE_MULTI_DOWNLOAD){
            fd.reDownLoad();
        }


    }

    /**
     * 删除文件就ok了
     */
    public void delete(){
        if(USE_AFINL_DOWNLOAD){

            if((saveDir != null) && (fileName!= null)){
                File file = new File(saveDir, fileName);

                if(file.exists()){
                    file.delete();
                }
            }

        }else if(USE_MULTI_DOWNLOAD){
            if(fd==null){
                fd = new MultiFileDownloader(mContext, url,saveDir,fileName,threadNum, null);
            }
            fd.delete();
        }

    }
}
